// pages/group_data/group_data.js
var common = require("../../common/common.js").common;

var QQMapWX = require("../../sdk/qqmap-wx-jssdk.min.js");

var qqmapsdk;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        array: [ "女", "男", "保密" ],
        index: 0,
        region: [ "江苏省", "南通市", "崇川区" ],
        info: [],
        address_value: [],
        sex_value: "",
        chooseAddress: "",
        latitude: "",
        longitude: "",
        address: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getData();
        common.getAppBase().then(() => {
            qqmapsdk = new QQMapWX({
                key: that.data.appBase.location_key
            });
        });
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    bindPickerChange: function(e) {
        this.setData({
            sex_value: e.detail.value
        });
    },
    // 选择省市区函数
    changeRegin(e) {
        this.setData({
            address_value: e.detail.value
        });
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Leader/leaderpersonal/leader_info",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    info: res.extra,
                    address_value: res.extra.address_name,
                    sex_value: res.extra.sex,
                    chooseAddress: res.extra.title,
                    address: res.extra.address
                });
            }
        });
    },
    getUserInfo(e) {},
    changeNickname(e) {
        console.log(e);
        this.setData({
            "info.nickname": e.detail.value
        });
    },
    changeName(e) {
        this.setData({
            "info.name": e.detail.value
        });
    },
    changPhone(e) {
        this.setData({
            "info.mobile": e.detail.value
        });
    },
    changeOther(e) {
        this.setData({
            "info.other": e.detail.value
        });
    },
    changeRemark(e) {
        this.setData({
            "info.remark": e.detail.value
        });
    },
    formSubmit: function(c) {
        let that = this;
        let e = {
            detail: {
                value: {
                    address: that.data.info.address,
                    address_name: that.data.address_value,
                    img: that.data.info.img,
                    mobile: that.data.info.mobile,
                    name: that.data.info.name,
                    nickname: that.data.info.nickname,
                    other: that.data.info.other,
                    remark: that.data.info.remark,
                    sex: that.data.sex_value,
                    title: that.data.chooseAddress
                }
            }
        };
        var openid = wx.getStorageSync("user").openid;
        wx.getUserProfile({
            desc: "用于完善会员资料",
            success: function(res) {
                console.log(res);
                let avatarUrl = "info.img";
                that.setData({
                    avatarUrl: JSON.parse(res.rawData).avatarUrl
                });
                e.detail.value.img = JSON.parse(res.rawData).avatarUrl;
                var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                var reg2 = /^[1][0-9][0-9]{9}$/;
                if (!reg2.test(e.detail.value.mobile)) {
                    let phoneerror = "手机号不对";
                    wx.showModal({
                        title: "提示",
                        content: phoneerror
                    });
                    return false;
                }
                common.request({
                    endpoint: "Leader/leaderpersonal/leader_info_change",
                    method: "post",
                    data: {
                        openid: openid,
                        img: e.detail.value.img,
                        nickname: e.detail.value.nickname,
                        name: e.detail.value.name,
                        mobile: e.detail.value.mobile,
                        usercard: e.detail.value.usercard,
                        address_name: e.detail.value.address_name,
                        title: e.detail.value.title,
                        other: e.detail.value.other,
                        remark: e.detail.value.remark,
                        address: e.detail.value.address,
                        sex: e.detail.value.sex
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        wx.showToast({
                            title: "保存成功",
                            icon: "success",
                            success: res => {
                                setTimeout(() => {
                                    wx.redirectTo({
                                        url: "/leaderPerson/pages/center/center"
                                    });
                                }, 500);
                            }
                        });
                    },
                    fail: res => {
                        wx.showModal({
                            title: "提示",
                            content: "保存失败",
                            showCancel: false,
                            success: function() {
                                return false;
                            }
                        });
                    }
                });
            }
        });
    },
    onChangeAddress: function() {
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                that.setData({
                    chooseAddress: res.name,
                    latitude: res.latitude,
                    longitude: res.longitude,
                    address: res.latitude + "," + res.longitude
                });
                that.getLocal(res.latitude, res.longitude);
            },
            fail: function(err) {}
        });
    },
    getLocal: function(latitude, longitude) {
        let that = this;
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: latitude,
                longitude: longitude
            },
            success: function(res) {
                let province = res.result.ad_info.province;
                let city = res.result.ad_info.city;
                let district = res.result.ad_info.district;
                that.setData({
                    address_value: [ province, city, district ]
                });
            },
            fail: function(res) {},
            complete: function(res) {}
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.leader_title ? res.extra.leader_title : "团长" + "个人资料"
                });
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    communityName: res.extra.community_title ? res.extra.community_title : "社区"
                });
            }
        });
    }
});